Essential MATLAB for Engineers and Scientists by Brian Hahn & Daniel T. Valentine

Essential MATLAB for Engineers and Scientists by Brian Hahn & Daniel T. Valentine

Author:Brian Hahn & Daniel T. Valentine
Language: eng
Format: epub
ISBN: 9780081008775
Publisher: Elsevier Science
Published: 2016-09-09T04:00:00+00:00


Figure 8.1 Projectile trajectory.

Note also the use of an if inside the while loop to ensure that only co-ordinates of points above the ground are added to the vectors xp and yp.

If you want the last point above the ground to be closer to the ground, try a smaller value of dt, e.g., 0.01.

8.2.6. break and continue

Any loop structure you are likely to encounter in scientific programming can be coded with either ‘pure’ for or while loops, as illustrated by the examples in this chapter. However, as a concession to intellectual laziness I feel obliged to mention in passing the break and continue statements.

If there are a number of different conditions to stop a while loop you may be tempted to use a for with the number of repetitions set to some accepted cut-off value (or even Inf) but enclosing if statements which break out of the for when the various conditions are met. Why is this not regarded as the best programming style? The reason is simply that when you read the code months later you will have to wade through the whole loop to find all the conditions to end it, rather than see them all paraded at the start of the loop in the while clause.

If you are going to insist on using break you will have to look it up in help for yourself!

The continue statement is somewhat less virulent than break …



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.